﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FrameWorkSong;
public class ResMgrExamples : MonoBehaviourSimplify
{
    ResLoader resLoader = new ResLoader();//资源管理器
    public override void OnBeforeDestroy()
    {
       
    }

    // Start is called before the first frame update
    void Start()
    {
        BugTest();
        //Delay(9, () => {
        //    resLoader.LoadAssetAsync<AudioClip>(AudioName.Shooting.ToString(), "Audios/Shooting", LoadClip);
        //});

    }
    void Test()
    {
        resLoader.LoadAssetAsync<AudioClip>(AudioName.Firefly.ToString(), "resources://Audios/Firefly", Clip => {
            Debug.Log(Clip.name);
        });
        resLoader.LoadAssetAsync<AudioClip>(AudioName.Firefly.ToString(), "resources://Audios/Firefly", Clip => {
            Debug.Log(Clip.name);
        });
    }
    void BugTest()
    {
        //AsyncLoadAB("gameobject");
        // SyncLoadAB("gameobject");
        SyncLoadAB("square");
        AsyncLoadABAsset("gameobject", "Game");
        //SyncLoadAB("gameobject");
        //SyncLoadABAsset("gameobject", "Game");
    }
    void  SyncLoadAB(string abName)
    {
        // var mianAB = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abName);
        var mianAB = resLoader.LoadAssetSync<AssetBundle>(abName, Application.streamingAssetsPath + "/" + abName);
      
    }

    /// <param name="abName">AB包名称</param>
    /// <param name="AssetName">资源名称</param>
    void SyncLoadABAsset(string abName,string AssetName)
    {
        var mianAB = resLoader.LoadAssetSync<GameObject>(abName, AssetName, Application.streamingAssetsPath + "/" + abName);
        GameObject game = Instantiate(mianAB);
    }
    void  AsyncLoadAB(string abName)
    {
        // var mianAB = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abName);
        resLoader.LoadAssetAsync<AssetBundle>(abName, Application.streamingAssetsPath + "/" + abName,  ab => { Debug.Log(ab.name); });
      
    }
    void AsyncLoadABAsset(string abName, string AssetName)
    {
        resLoader.LoadAssetAsync<GameObject>(abName, AssetName, Application.streamingAssetsPath + "/" + abName, ab => { GameObject game = Instantiate(ab); });
    }
    /// <summary>
    /// 加载测试
    /// </summary>
    void LoadTest()
    {
        Delay(1, () => AudioManager.Instance.PlaySound(AudioName.Shooting));
        Delay(2, () => { resLoader.LoadAssetSync<GameObject>("Cube", "resources://Cube"); });
        Delay(3, () => {
            GameObject game = Instantiate(resLoader.LoadAssetSync<GameObject>("Sphere", "resources://Sphere"));
            resLoader.ReleaseAll();
            Delay(5, () =>
            {
                ReleaseMemory();
                Debug.Log(game);
            });
        });
        Delay(8, () => {
            resLoader.LoadAssetAsync<AudioClip>(AudioName.Firefly.ToString(), "resources://Audios/Firefly", Clip => {
                Debug.Log(Clip.name);
            });
        });
    }
    void  LoadClip(AudioClip audioClip)
    {
        Debug.Log(audioClip.name);
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            resLoader.ReleaseAll();
        }
    }
}
